/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: getSumFunction.c 7805 2014-03-13 09:54:35Z geisinger $
 */

/**
 * \file
 *         Source file for function getSum in component system.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "rOSGateway/adv/system/include/getSumFunction.h"

#include "rOSGateway/adv/system/include/getSumFunctionWrapper.h"
#include "rOSGateway/adv/system/include/systemComponent.h"
#include "rOSGateway/adv/system/include/systemComponentWrapper.h"
#include "rOSGateway/adv/system/include/systemManifest.h"

#include "xme/core/logUtils.h"

#include "xme/hal/include/mem.h"

// PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_C_INCLUDES) ENABLED START
// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_C_DEFINITIONS) ENABLED START
// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/
/**
 * \brief  Variable holding the value of the required output port 'sendSum'.
 *
 * \details If necessary initialize this in the init function.
 *          The value of this variable will be written to the port at the end of
 *          the step function.
 */
static ROSGateway_topic_sumResponse_t
portSendSumData;

// PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_C_VARIABLES) ENABLED START
// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_C_PROTOTYPES) ENABLED START
// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
rOSGateway_adv_system_getSumFunction_init
(
    rOSGateway_adv_system_systemComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_INITIALIZE_C) ENABLED START
    XME_UNUSED_PARAMETER(componentConfig);
    
    return XME_STATUS_SUCCESS;
    // PROTECTED REGION END
}

void
rOSGateway_adv_system_getSumFunction_step
(
    rOSGateway_adv_system_systemComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    ROSGateway_topic_sumRequest_t portGetSumData; // Required port.
    ROSGateway_topic_sumResponse_t* portSendSumDataPtr = &portSendSumData;
    xme_core_component_requestDataHandle_t portGetSumRequestDataHandle = (xme_core_component_requestDataHandle_t)XME_CORE_COMPONENT_INVALID_REQUEST_DATA_HANDLE;
    
    (void)xme_hal_mem_set(&portGetSumData, 0u, sizeof(ROSGateway_topic_sumRequest_t));
    
    status[0] = rOSGateway_adv_system_systemComponentWrapper_readRequestHandlerPortGetSum(&portGetSumData, &portGetSumRequestDataHandle);
    
    {
        // PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_STEP_C) ENABLED START
        XME_UNUSED_PARAMETER(componentConfig);

        xme_fallback_printf("System: getSum\n");

        portSendSumData.sum = portGetSumData.a + portGetSumData.b;
        // PROTECTED REGION END
    }
    
    status[0] = rOSGateway_adv_system_systemComponentWrapper_writeResponseSenderPortSendSum(portSendSumDataPtr, &portGetSumRequestDataHandle);
    
    {
        // PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_STEP_2_C) ENABLED START
    	if (XME_STATUS_SUCCESS != status[0])
		{
			XME_LOG(XME_LOG_ERROR, "getSumFunction: Error sending response!\n");
		}
    	// PROTECTED REGION END
    }
}

void
rOSGateway_adv_system_getSumFunction_fini
(
    rOSGateway_adv_system_systemComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_TERMINATE_C) ENABLED START
    XME_UNUSED_PARAMETER(componentConfig);
    // PROTECTED REGION END
}

// PROTECTED REGION ID(ROSGATEWAY_ADV_SYSTEM_GETSUMFUNCTION_IMPLEMENTATION_C) ENABLED START
// PROTECTED REGION END
